package pers.andy.websocket.tomcat;

import java.util.List;
import java.util.Map;

import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

/**
 * 握手拦截器，用于处理请求携带参数
 * @author Andy.ye
 * @date 2018年4月8日 下午5:13:20
 */
public class WebSocketHandshakeInterceptor extends ServerEndpointConfig.Configurator {

	@Override
	public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) {
		Map<String, List<String>> paramterMap = request.getParameterMap();
		List<String> randStrs = paramterMap.get("randStr");
		List<String> roles = paramterMap.get("role");
		if (randStrs != null && !randStrs.isEmpty()) {
			config.getUserProperties().put("randStr", randStrs.get(0));
		}
		if (roles != null && !roles.isEmpty()) {
			config.getUserProperties().put("role", roles.get(0));
		}
	}
}
